package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends BaseServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult findPage(SensitiveDto dto) {
        if (dto==null)return paramError;
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        Page<AdSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        if (StringUtils.isNoneBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage<AdSensitive> page1 = page(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page1.getTotal());
        result.setData(page1.getRecords());
        return result;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive==null || StringUtils.isBlank(adSensitive.getSensitives())){
            return paramError;
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return paramSuccess;
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive==null || adSensitive.getId()==null){
            return paramError;
        }
        updateById(adSensitive);
        return paramSuccess;
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id==null){
            return paramError;
        }
        removeById(id);
        return paramSuccess;
    }
}
